package com.jisf.driver.auth;

import com.jisf.driver.response.Result;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Classname MyAuthenticationSuccessHandler
 * @Description TODO 登录成功时的处理器
 * @Date 2022/3/20 23:20
 * @Created by jisf：【429627912@qq.com】
 */
@Component("myAuthenticationSuccessHandler")
public class MyAuthenticationSuccessHandler extends JsonAuthentication implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        //返回json数据

        SecurityContextHolder.getContext().setAuthentication(authentication);

        Object principal = authentication.getPrincipal();



        Result result = Result.ok(200, "登陆成功");
//        response.setContentType("text/json;charset=utf-8");
//        response.getWriter().write(new ObjectMapper().writeValueAsString(result));
        this.WriteJSON(request, response, result);
    }
}
